import fs from 'fs';
import path from 'path';
import querystring from 'querystring';

function newuser(req,res){
    //接受来自前端的数据请求；
    let buf=Buffer.alloc(0);
    req.on('data',chunk=>{
        buf=Buffer.concat([buf,chunk ])
    })
    req.on('end',()=>{
        //获取user.json;
        let filePath=path.join(__dirname,'../data/user.json')
        let {id,us}=querystring.parse(buf.toString());//键值对
        // console.log(querystring.parse(buf.toString()))
        us=JSON.parse(us)
        let list=JSON.parse(fs.readFileSync(filePath))
        
        list[id].name=us.name;
        list[id].sex=us.sex;
        list[id].address=us.address;
        list[id].tel=us.tel;
        list[id].card=us.card;
        console.log(list[id])
        fs.writeFileSync(filePath,JSON.stringify(list),'utf-8')
        res.send('1')
    })
}
export {newuser}